package com.example.bai4;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {
	static ArrayList<Employee> arr;
	TextView edtempcd;
	TextView edtempnm;
	RadioGroup rdggender;
	MyAdapter adapter;
	ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        arr= new ArrayList<Employee>();
        
        edtempcd=(TextView) findViewById(R.id.edtempcd);
        edtempnm=(TextView) findViewById(R.id.edtempnm);
       
        ImageButton btndelete= (ImageButton) findViewById(R.id.btndelete);
       Button btninsert=(Button) findViewById(R.id.btninsert);
       
        rdggender= (RadioGroup) findViewById(R.id.group_gender);
        list= (ListView) findViewById(R.id.list);
       
        adapter= new MyAdapter(this, R.layout.my_item_layout, arr);
       list.setAdapter(adapter);
       
	       btninsert.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				insertItem();
			}
		});
	       btndelete.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				deleteItem();
			}
		});
    }
    
    private void insertItem()
    {
    	Employee e= new Employee();
    	e.setId(edtempcd.getText().toString());
    	e.setName(edtempnm.getText().toString());
    	int i=rdggender.getCheckedRadioButtonId();
    	if(i== R.id.radfemale)
    		e.setGender(false);
    	else
    		e.setGender(true);
    	arr.add(e);
    	adapter.notifyDataSetChanged();
    	
    	edtempcd.setText("");
    	edtempnm.setText("");
    	//edtempcd.requestFocus();
    }
    
    private void deleteItem()
    {
    	// di tu dau -> cuoi
    	for(int i=0;i<list.getChildCount();i++)
    	{
    		View v=list.getChildAt(i);
    		CheckBox c= (CheckBox) v.findViewById(R.id.chkitem);
    		if(c.isChecked())
    		{
    		arr.remove(i);
        	adapter.notifyDataSetChanged();
    		}
    	}
    	
    }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
